package com.basketball.task;

import com.basketball.entity.ClubDate;
import com.basketball.entity.Player;
import com.basketball.entity.Record;
import com.basketball.service.ClubDateService;
import com.basketball.service.MailService;
import com.basketball.service.PlayerService;
import com.basketball.service.RecordService;
import com.basketball.util.IpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class ConclusionTask {
    public final static Logger logger = LoggerFactory.getLogger(ConclusionTask.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private PlayerService playerService;
    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private MailService mailService;
    @Autowired
    private ClubDateService clubDateService;
    @Autowired
    private RecordService recordService;
    //@Scheduled(cron = "*/10 * * * * ?")
    @Scheduled(cron = "0 40 10 * * ?")
    private void process() {
        logger.info("【活动统计】定时器开始，现在时间：" + dateFormat.format(new Date()));
        int week_index = canSendList();
        if (week_index != 0) {
            sendMail(week_index);
        }
    }

    /**
     * 判断是否为活动日期当天
     *
     * @return
     */
    private int canSendList() {
        List<ClubDate> list = clubDateService.getAll();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int week_index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        for (int i = 0; i < list.size(); i++) {
            ClubDate clubDate = list.get(i);
            if (week_index == clubDate.getDate()) {
                logger.info(i + "今天是星期：" + week_index + "可以发送【活动统计】邮件");
                return week_index;
            }
        }
        logger.info("今天是星期：" + week_index + "无需发送统计邮件");
        return 0;
    }

    public void sendMail(int week_index) {
        List<Player> playerList = playerService.getByState();
        for (int i = 0; i < playerList.size(); i++) {
            Player player = playerList.get(i);
            logger.info("发送【活动统计】邮件给" + player.getName() + "-" + player.getEmail()+ "完成" );
            sendTemplateMail(player.getEmail(), player.getName(), week_index, player.getSec());
        }
        //logger.info("邮件全部发送完毕" + dateFormat.format(new Date()));
    }

    public void sendTemplateMail(String email, String name, int week_index, String seckey) {
        logger.info(email, name);
        //创建邮件正文
        Context context = new Context();
        context.setVariable("name", name);
        context.setVariable("week_index", week_index);
        context.setVariable("seckey", seckey);
        context.setVariable("ip", IpUtil.getIpAddress());
        logger.info("【活动统计】dateFormat.format(new Date())------" + dateFormat.format(new Date()));
        List<Record> recordList = recordService.getRecordByDate(dateFormat.format(new Date()));
        String players = "";
        for (int i = 0; i < recordList.size(); i++) {
            Record record = recordList.get(i);
            players += record.getName() + "      ";
        }
        context.setVariable("recordList", players);
        if (recordList.size() < 6) {
            context.setVariable("conclusion", "本次活动小于6人，存在人员不足风险");
        } else {
            context.setVariable("conclusion", "本次活动人数为" + recordList.size() + "人，可正常组织活动！");
        }
        String emailContent = templateEngine.process("playerListTemplate", context);
        mailService.sendHtmlMail(email, "篮球活动人员名单", emailContent);
    }
}
